package Leave

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/models"
	"project/pkg/validate"
)

func (h LHandler) SearchAllToStudent(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.SearchAllToStudentReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:Update with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:Update with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//判断是否是只差找学生请求状态下的
	if req.NoConfirm == 0 {
		info, count, err := h.M.LeaveQueryUserId(req.WxOpenID, req.Page, req.Size)
		if err != nil {
			zap.L().Error("Leave:SearchAllToStudent h.M.LeaveQueryUserId err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.Data = info
		resp.Count = count
		resp.SendSuccess(c)
		return
	} else {
		info, count, err := h.M.LeaveQueryUserIdAndNo(req.WxOpenID, req.Page, req.Size)
		if err != nil {
			zap.L().Error("Leave:SearchAllToStudent h.M.LeaveQueryUserId err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.Data = info
		resp.Count = count
		resp.SendSuccess(c)
		return
	}
}
